package indexer

import com.mongodb.MongoClient
import com.mongodb.MongoWriteException
import com.mongodb.client.model.InsertOneOptions
import org.bson.Document
import org.slf4j.LoggerFactory
import us.codecraft.webmagic.ResultItems
import us.codecraft.webmagic.Task
import us.codecraft.webmagic.pipeline.Pipeline
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class MongoPipeline(val mongoClient: MongoClient) : Pipeline {

    var database = mongoClient.getDatabase("book")
    var collection = database.getCollection("bc")
    override fun process(resultItems: ResultItems, task: Task) {
        val up = InsertOneOptions()
        //up.upsert(true)
        val name:String = resultItems["name"]
        val url:String = resultItems["url"]
        // val tags:String = resultItems["tags"]
        val author: String = resultItems["author"]

        try{
            collection.insertOne(Document("_id", name).
                    append("name", name)
                    .append("url",url)
                    .append("author", author)
                    .append("created", LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME)))
        }catch (e:Exception){
            if (e is MongoWriteException){
                val we = e.error
                if(we.code == 11000){
                    return
                }
            }
            Log.error("",e)
        }
    }

    val Log = LoggerFactory.getLogger(javaClass)


}